/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.Impl;

import dao.AccountDao;
import dao.PetDao;
import entity.Pet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ZZ
 */
public class PetDaoImpl extends BaseDao implements PetDao{

    @Override
    public int add(Pet pet) {
        int r=0;
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement ppst=con.prepareStatement("insert into pet(ID,NAME,TYPENAME,HEALTH,LOVE,BIRTHDAY,OWNER_ID,STORE_ID,PRICE) VALUES(?,?,?,?,?,?,?,?,?)");
            ppst.setInt(1, pet.getId());
            ppst.setString(2,pet.getName());
            ppst.setString(3,pet.getTypeName());
            ppst.setInt(4,pet.getHealth());
            ppst.setInt(5,pet.getLove());
            ppst.setString(6,pet.getBirthday());
            ppst.setInt(7,pet.getOwner_ID());
            ppst.setInt(8,pet.getStore_ID());
            ppst.setDouble(9,pet.getPrice());
            r=ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
        
    }

    @Override
    public Boolean move(int id) {
        try {
            Connection con=BaseDao.getConnection();
            PreparedStatement ppst=con.prepareStatement("delete from pet WHERE ID="+id);
            ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
      return true;
    }

    @Override
    public void update(int id, int owner_id) {
         try {
            Connection con=BaseDao.getConnection();
            PreparedStatement ppst=con.prepareStatement("update pet set OWNER_ID=? WHERE ID="+id);
            ppst.setInt(1,owner_id);
            ppst.executeUpdate();
            ppst.close();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public List<Pet> select() {
        Pet pet;
        List child = new ArrayList();
        try {
            Statement statement=BaseDao.getConnection().createStatement();
            ResultSet rs=statement.executeQuery("select * from pet");
            while (rs.next()) {
                pet=new Pet(rs.getInt("ID"), 
                        rs.getString("NAME"),
                        rs.getString("TYPENAME"),
                        rs.getInt("HEALTH"),
                        rs.getInt("LOVE"), 
                        rs.getString("BIRTHDAY"), 
                        rs.getInt("OWNER_ID"),
                        rs.getInt("STORE_ID"),
                        rs.getDouble("ID"));
                
                child.add(pet);
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(PetDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return child;
        
        
    }
    
}
